package aliyun

import (
	"context"
	"fmt"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
	"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
	"github.com/spf13/viper"
)

func PutObject(c context.Context, path, ossPath string) (string, error) {
	// 加载默认配置并设置凭证提供者和区域
	cfg := oss.LoadDefaultConfig().
		WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
		WithRegion(viper.GetString("aliyun.oss.region"))

	// 创建OSS客户端
	client := oss.NewClient(cfg)
	fmt.Println(viper.GetString("aliyun.oss.bucket_name"))
	// 创建上传对象的请求
	putRequest := &oss.PutObjectRequest{
		Bucket:       oss.Ptr(viper.GetString("aliyun.oss.bucket_name")), // 存储空间名称
		Key:          oss.Ptr(ossPath),                                   // 对象名称
		StorageClass: oss.StorageClassStandard,                           // 指定对象的存储类型为标准存储
	}

	// 执行上传对象的请求
	_, err := client.PutObjectFromFile(c, putRequest, path)
	if err != nil {
		return "", err
	}

	return viper.GetString("aliyun.oss.http_url") + ossPath, nil
}
